import { useAuth } from '../contexts/AuthContext';

/**
 * Custom hook to check user roles and permissions
 * Uses Appwrite labels for role-based access control
 */
export const useUserRole = () => {
  const { user, isAdmin } = useAuth();

  /**
   * Check if user has a specific label/role
   * @param label - The label to check for (e.g., 'admin', 'subscriber', 'vip')
   * @returns boolean indicating if user has the label
   */
  const hasLabel = (label: string): boolean => {
    return user?.labels?.includes(label) || false;
  };

  /**
   * Check if user has any of the specified labels
   * @param labels - Array of labels to check
   * @returns boolean indicating if user has at least one of the labels
   */
  const hasAnyLabel = (labels: string[]): boolean => {
    return labels.some(label => hasLabel(label));
  };

  /**
   * Check if user has all of the specified labels
   * @param labels - Array of labels to check
   * @returns boolean indicating if user has all the labels
   */
  const hasAllLabels = (labels: string[]): boolean => {
    return labels.every(label => hasLabel(label));
  };

  /**
   * Get all user labels
   * @returns Array of user labels
   */
  const getUserLabels = (): string[] => {
    return user?.labels || [];
  };

  return {
    isAdmin,
    hasLabel,
    hasAnyLabel,
    hasAllLabels,
    getUserLabels,
  };
};
